//
//  IKEcgRealTimeCheckController.m
//  Ikinloop-demo
//
//  Created by Li Shen on 2021/4/21.
//  Copyright © 2021 lyh. All rights reserved.
//

#import "EcgCheckOrBindController.h"
#import "EcgDeviceScanViewController.h"
#import "EcgRealTimeCheckViewController.h"

@interface EcgCheckOrBindController ()

@property (nonatomic, strong) EcgRealTimeCheckViewController *checkVc;

@property (nonatomic, strong) EcgDeviceScanViewController *bindVc;

@property (nonatomic, strong) UIViewController *currentVc;

@property (nonatomic, assign) BOOL blind;

@property (nonatomic,strong)id<IKEcgCheckInterface> delegate;
@property (nonatomic, strong) NSString* customData;

@end

@implementation EcgCheckOrBindController

- (instancetype)initWithDelegate:(id<IKEcgCheckInterface>)delegate customData:(NSString*)customData{
    self  = [super init];
    if(self){
        self.delegate = delegate;
        self.customData = customData;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.title = @"心电检测";
    [self addChildViewcontrollers];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self showEcgView];
}


-(void)addChildViewcontrollers
{
    self.checkVc = [[EcgRealTimeCheckViewController alloc] init];
    self.checkVc.view.frame = self.view.frame;
    self.checkVc.delegate = self.delegate;
    self.checkVc.customData = self.customData;
    [self addChildViewController:self.checkVc];

    self.bindVc = [[EcgDeviceScanViewController alloc] init];
    self.bindVc.view.frame = self.view.frame;
    [self addChildViewController:self.bindVc];
    
    [self.view addSubview:self.bindVc.view];
    self.currentVc = self.bindVc;
}

- (void)showEcgView{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *deviceId =  [userDefaults objectForKey:@"key_deviceId"];
    if ([IKPublicUtil isEmpty:deviceId])
    {
        self.blind = YES;
        [self pushNoblind];
    }
    else
    {
        self.blind = NO;
        [self pushBlind];
    }
    [self setNeedsStatusBarAppearanceUpdate];
}

- (void)replaceController:(UIViewController *)oldController newController:(UIViewController *)newController
{
    [self addChildViewController:newController];
    [self transitionFromViewController:oldController toViewController:newController duration:0 options:UIViewAnimationOptionTransitionNone animations:nil completion:^(BOOL finished) {
        
        if (finished) {
            
            [newController didMoveToParentViewController:self];
            [oldController willMoveToParentViewController:nil];
            [oldController removeFromParentViewController];
            self.currentVc = newController;
            [self.currentVc.view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.equalTo(self.view);
            }];
            
        }else{
            
            self.currentVc = oldController;
            
        }
    }];
}


- (void)pushBlind{
    if ([self.currentVc isEqual:self.checkVc])
    {
        return;
    }
    self.title = @"心电检测";
    [self replaceController:self.currentVc newController:self.checkVc];
}

-(void)pushNoblind
{
    if ([self.currentVc isEqual:self.bindVc])
    {
        return;
    }
    self.title = @"绑定设备";
    [self replaceController:self.currentVc newController:self.bindVc];
}

@end
